package com.martian.controller;

import com.martian.common.exception.MovieException;
import com.martian.common.exception.SysException;
import com.martian.common.util.ThreadLocalMap;
import com.martian.common.wrapper.ResultWrapper;
import com.martian.pojo.form.BatchDeleteMovieForm;
import com.martian.pojo.form.DeleteMovieForm;
import com.martian.pojo.form.LoadMovieForm;
import com.martian.pojo.form.MovieRatingForm;
import com.martian.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Null;
import java.util.List;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-03-20 13:55
 * @TODO 为重要点击事件设置redis缓存
 */
@CrossOrigin
@Api(value = "MovieController", tags = {"电影API"})
@RestController
@RequestMapping("/movie")
public class MovieController {
    @Resource
    MovieService movieService;

    @ApiOperation(value = "分页返回所有电影", produces = "application/json")
    @GetMapping("/getAllMovieByPage")
    public ResultWrapper getAllMovieByPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        try {
            return ResultWrapper.successWithData(movieService.getAllMovieByPage(page, pageSize));
        }catch (SysException e){
            return ResultWrapper.failure(e.getMessage());
        }

    }
    @ApiOperation(value = "搜索指定名称或类别的电影", produces = "application/json")
    @GetMapping("/visitor/searchMovieByAny")
    public ResultWrapper getMovieByNameOrGenre(@RequestParam Integer page,@RequestParam Integer pageSize,@RequestParam(required = false) String info, @RequestParam(required = false) Integer searchType){
        return ResultWrapper.successWithData(movieService.getMovieByNameOrGenre(page, pageSize, info, searchType));
    }
//    下面的方法最好将更新和添加用Put和Post请求区分开
    @ApiOperation(value = "新增或修改电影", produces = "application/json")
    @PostMapping("/insertOrEditMovie")
    public ResultWrapper insertOrEditMovie(@Valid @RequestBody LoadMovieForm loadMovieForm){
        System.out.println(loadMovieForm.getMovieId());
        movieService.insertMovie(loadMovieForm);
        return ResultWrapper.success("新增电影成功");
    }
    @ApiOperation(value = "删除电影", produces = "application/json")
    @DeleteMapping("/deleteMovie/{movieId}")
    public ResultWrapper deleteMovie(@PathVariable Integer movieId){
        movieService.deleteMovie(movieId);
        return ResultWrapper.success("删除电影成功");
    }
    @ApiOperation(value = "批量删除电影", produces = "application/json")
    @PostMapping("/batchDelete")
    public ResultWrapper batchDelete(@Valid @RequestBody List<Integer> batchDeleteMovieId){
        movieService.batchDeleteMovies(batchDeleteMovieId);
        return ResultWrapper.success("批量删除电影成功");
    }
    @ApiOperation(value = "返回热门电影",produces = "application/json")
    @GetMapping("/visitor/getHotMovies")
    public ResultWrapper getHotMovies(@RequestParam Integer size){
        return ResultWrapper.successWithData(movieService.getHotMovies(size));
    }

    @ApiOperation(value = "返回与该电影相似的其他电影",produces = "application/json")
    @GetMapping("/visitor/getSimilarMovie")
    public ResultWrapper getSimilarMovie(@RequestParam Integer movieId,@RequestParam Integer size){
        try{
            return ResultWrapper.successWithData(movieService.getSimilarMovie(movieId,size));
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "返回对应电影",produces = "application/json")
    @GetMapping("/mayBeVisitor/getMovieById")
    public ResultWrapper getMovieById(@RequestParam Integer movieId){
        try{
//            System.out.println("ssss:"+ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER"));
            return ResultWrapper.successWithData(movieService.getMovieById(movieId));
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "收藏电影",produces = "application/json")
    @PostMapping("/collectMovie")
    public ResultWrapper collectMovie(@RequestBody Integer movieId){
        try{
            String info = movieService.collectMovie(movieId);
            return ResultWrapper.success(info);
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "电影评分",produces = "application/json")
    @PostMapping("/ratingMovie")
    public ResultWrapper ratingMovie(@RequestBody MovieRatingForm movieRatingForm){
        try{
            movieService.ratingMovie(movieRatingForm);
            return ResultWrapper.success("评分成功");
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询电影评分",produces = "application/json")
    @GetMapping("/selectMovieRating")
    public ResultWrapper selectMovieRating(@RequestParam Integer movieId){
        try{
            Double rating = movieService.selectMovieRating(movieId);
            return ResultWrapper.successWithData(rating);
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
    @ApiOperation(value = "查询电影是否被收藏",produces = "application/json")
    @GetMapping("/isCollectMovie")
    public ResultWrapper isCollectMovie(@RequestParam Integer movieId){
        try{
            Integer info = movieService.icCollectMovie(movieId);
            return ResultWrapper.successWithData(info);
        }catch (MovieException e){
            return ResultWrapper.failure(e.getMessage());
        }
    }
}
